################################################################################
#this is a script to generate summary of linear models and plot related figures

################################################################################
#define an output directory
out.dir = "C:/R/course/data/output/"

################################################################################
#create some data to play with
ID = rep(c("t1","t2","t3","t4","t5"),100)
x = runif(500,min=0,max=100)
fish = c(1:500)
temperature = 2*x + fish
indata = data.frame(ID,x,fish,temperature)

################################################################################
#Exercise 1: For group "t1", build a linear model for the relationship between
#fish and temperature. Export the model summary (intercept, slope, r squared) as a .csv file

#subset dataframe to only include rows where ID equals "t1"
tdata=subset(indata, ID=="t1")

#build linear model
lm1 = lm(fish~temperature, data=tdata)

#get out components of model summary
ID="t1"
Intercept=coefficients(lm1)[1]
slope=coefficients(lm1)[2]
Rsqr=summary(lm1)$r.squared

#create a data frame
outdata=data.frame(ID=ID, Intercept=Intercept, slope=slope, Rsqr=Rsqr)

#export data frame
write.csv(x=outdata,file=paste(out.dir, "output_t1_only.csv", sep=""), row.names=F)

################################################################################
#Exercise 2: For group "t1", plot the relationship between fish and temperature.
#Add a line of best fit from linear model above and export figure as a .png file

#set specifications of the plotting space and path to where output will be saved
png(filename=paste(out.dir, "plot_t1_only.png", sep=""), units="cm", res=100, height=20, width=20, bg="white")

#create the plot
plot(tdata$temperature, tdata$fish, xlab="temperature", ylab="fish")

#add line of best fit from linear model
abline(lm1,col="red")

#Turn off device driver (to flush output)
dev.off()

################################################################################
#Exercise 3: repeat the tasks above but this time using a single loop to work through
#summaries for each unique ID (i.e. treatment)

#get a list of treatments
unique.id=as.character(unique(indata$ID))

#define some output datasets
outdata = NULL

for (treatment in unique.id) {

  #subset dataframe to only include rows where ID equals treatment
  tdata=subset(indata, ID==treatment)
  #build linear model
  lm1 = lm(fish~temperature, data=tdata)
  #store output data
  outdata=rbind(outdata, data.frame(ID=treatment, Intercept=coefficients(lm1)[1], slope=coefficients(lm1)[2], Rsqr=summary(lm1)$r.squared))

  #set specifications of the plotting space and path to where output will be saved
  png(filename=paste(out.dir, "plot_", treatment, ".png", sep=""), units="cm", res=100, height=20, width=20, bg="white")
  #create the plot
  plot(tdata$temperature, tdata$fish, xlab="temperature", ylab="fish")
  #add line of best fit from linear model
  abline(lm1,col="red")
  #Turn off device driver (to flush output)
  dev.off()

}

#export data frame
write.csv(x=outdata,file=paste(out.dir, "output_all.csv", sep=""), row.names=F)